using System;
using System.Collections.Generic;

public class EventCentre : MonoSingleton<EventCentre>
{
	private Dictionary<object, Delegate> Dict_Events = new Dictionary<object, Delegate>();

	public void Subscribe(object name, Action action)
	{
		if (Dict_Events.ContainsKey(name))
		{
			Dict_Events[name] = Delegate.Combine(Dict_Events[name], action);
		}
		else
		{
			Dict_Events.Add(name, action);
		}
	}

	public void Subscribe<T>(object name, Action<T> action)
	{
		if (Dict_Events.ContainsKey(name))
		{
			Dict_Events[name] = Delegate.Combine(Dict_Events[name], action);
		}
		else
		{
			Dict_Events.Add(name, action);
		}
	}

	public void UnSubscribe(object name, Action action)
	{
		if (Dict_Events.ContainsKey(name))
		{
			Dict_Events[name] = Delegate.Remove(Dict_Events[name], action);
		}
	}

	public void UnSubscribe<T>(object name, Action<T> action)
	{
		if (Dict_Events.ContainsKey(name))
		{
			Dict_Events[name] = Delegate.Remove(Dict_Events[name], action);
		}
	}

	public void Trigger(object name)
	{
		if (Dict_Events.TryGetValue(name, out var value))
		{
			(value as Action)?.Invoke();
		}
	}

	public void Trigger<T>(object name, T value1)
	{
		if (Dict_Events.ContainsKey(name))
		{
			(Dict_Events[name] as Action<T>)?.Invoke(value1);
		}
	}

	public void RemoveAll()
	{
		Dict_Events.Clear();
	}

	private void OnDestroy()
	{
		RemoveAll();
	}
}
